/**
 * Title:        流程版本信息表数据检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.GCONST;
import weaver.general.OrderProperties;
import weaver.general.Util;
import weaver.system.ThreadWorkTimer;
import weaver.system.WrokflowOverTimeTimer;



public class CheckOvertime extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("CheckOvertime  start");

        recordSet.executeProc("GetDBDateAndTime","");
        
        List resultList = new ArrayList();
        String result = "";
        String msg = "";
        String currentdate ="";
        String currenttime ="";
        if(recordSet.next()){
            currentdate = recordSet.getString("dbdate");
            currenttime = recordSet.getString("dbtime");
        }
        String overtime =null2String(getPropValue("weaver","ecology.overtime"));
        if(!"".equals(overtime)) {
            if(getIntValue(overtime) <= 0){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","超时扫描频率非数字。");
                resultMap.put("linkurl","/workflow/WorkflowSettings.jsp");

                resultMap.put("inputnamelist","overtime");
                resultMap.put("buttonname","修复");
                resultList.add(resultMap);
            }else if(getthreadTime() != getIntValue(overtime)){
                result = resultNeedFix;
                resultList.add("超时扫描频率(" + overtime + "分钟)和线程中(" + getthreadTime() + "分钟)不一致，请重启服务。");
            }else{
                resultList.add("超时扫描频率："+overtime+"分钟！");
                String wfovertimetmp =null2String(getPropValue("workflowovertime","WORKFLOWOVERTIMETEMP"));
                if("1".equals(wfovertimetmp)) {
                    recordSet.executeSql("select * from HrmSchedule WHERE relatedid = 0 and validedateto > '"+currentdate+"' ");
                    if(recordSet.next()){
                        result = resultOk;
                        resultList.add("开启了过滤非工作时间，并设置了一般工作时间，测试正常!");
                    }else{
                        result = resultNeedFix;
                        Map resultMap = new HashMap<String,String>();
                        resultMap.put("showMessage","开启了过滤非工作时间，一般工作时间没有设置或者设置不是总部或者时间过期");
                        resultMap.put("linkurl","/hrm/schedule/HrmDefaultSechedule_frm.jsp");
                        resultMap.put("linkname","一般工作时间");
                        resultList.add(resultMap);
                    }
                }else{
                    result = resultOk;
                    resultList.add("没有开启过滤非工作时间，测试正常!");
                }
            }
       }else{
           result = resultNeedFix;
           Map resultMap = new HashMap<String,String>();
           resultMap.put("showMessage","没有启用超时功能。");
           resultMap.put("linkurl","/workflow/WorkflowSettings.jsp");

           resultList.add(resultMap);
       }
        (new BaseBean()).writeLog("CheckOvertime  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        
        
        setRecordSet();
        (new BaseBean()).writeLog("CheckOvertime  start");

        List resultList = new ArrayList();
        String result = resultOk;
        String divid = (String)dataMap.get("divid");
        Map resultMap = new HashMap<String,String>();
        resultMap.put("divid",divid);
        int overtimetime = Util.getIntValue((String)dataMap.get("overtime"));
        if(dataMap == null || dataMap.get("overtime") == null){
            overtimetime = 10;
        }
        if(overtimetime <= 0){
            result = resultNeedFix;
            resultMap.put("result",resultNeedFix);
            resultMap.put("fixMessage","超时时间修复失败");
            resultList.add(resultMap);
            return formatReturnJsonCheck(result, resultList,this);
        }
        OrderProperties overtimeprop = new OrderProperties();
        overtimeprop.load(GCONST.getPropertyPath()+ "weaver.properties");
        overtimeprop.put("ecology.overtime", "" + overtimetime);
        overtimeprop.store(overtimeprop, GCONST.getPropertyPath() + "weaver.properties");
        result = resultOk;
        resultMap.put("result",resultOk);
        resultMap.put("fixMessage","超时时间修复成功");
        resultList.add(resultMap);
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "超时功能检测";
    }


    private long getthreadTime(){

        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup topGroup = group;
        // 遍历线程组树，获取根线程组  
        while (group != null) {
            topGroup = group;
            group = group.getParent();
        }
        // 激活的线程数加倍  
        int estimatedSize = topGroup.activeCount() * 2;
        Thread[] slackList = new Thread[estimatedSize];
        // 获取根线程组的所有线程  
        int actualSize = topGroup.enumerate(slackList);
        // copy into a list that is the exact size  
        Thread[] list = new Thread[actualSize];
        System.arraycopy(slackList, 0, list, 0, actualSize);

        long timelimit = 0l;
        try{
            for (Thread thread : list) {
                Object tid = thread.toString();
                Class<?> rthreadClazz = thread.getClass();
                String rthreadDesc = "";
                Class<?> clazz = thread.getClass();
                if (clazz.equals(ThreadWorkTimer.class)) {
                    ThreadWorkTimer twt = (ThreadWorkTimer) thread;
                    Field field = clazz.getDeclaredField("work");
                    field.setAccessible(true);
                    Object obj = field.get(twt);
                    
                    Field timefield = clazz.getDeclaredField("m_timer");
                    timefield.setAccessible(true);
                    if (rthreadClazz.equals(WrokflowOverTimeTimer.class)) {
                        timelimit = (Long)timefield.get(twt);
                        break;
                    }
                }
            }
        }catch(Exception e){
            
        }
        return timelimit/1000/60;
    }

    @Override
    public String getClassDesc() {
        // TODO Auto-generated method stub
        return "流程超时设置检测，批量修复时，按照10分钟进行设定";
    }

}



